﻿using BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GGSDDU
{
    public partial class Form1 : Form
    {
        #region 变量

        GDBll bll;
        DateTime startTime;

        int leftMinutes;
        int LeftMinutes
        {
            get { return this.leftMinutes; }
            set
            {
                this.leftMinutes = value;
                this.lblLeftMinutes.Text = this.leftMinutes.ToString();
            }
        }
        Task taskStart;

        FrmModel enmFrmMode = FrmModel.Pause;
        FrmModel EnmFrmMode
        {
            get { return this.enmFrmMode; }
            set
            {
                if (this.enmFrmMode == value)
                {
                    return;
                }

                this.enmFrmMode = value;
                switch (this.enmFrmMode)
                {
                    case FrmModel.Pause:
                        this.Pause();
                        break;
                    case FrmModel.Start:
                        this.Start();
                        break;
                }
            }
        }

        #endregion

        /// <summary>
        /// 构造函数
        /// </summary>
        public Form1()
        {
            InitializeComponent();
        }

        #region 事件

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Init();
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.EnmFrmMode = FrmModel.Pause;
        }

        /// <summary>
        /// 按钮按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpe_Click(object sender, EventArgs e)
        {
            if (this.EnmFrmMode != FrmModel.Start)
            {
                this.EnmFrmMode = FrmModel.Start;
            }
            else
            {
                this.EnmFrmMode = FrmModel.Pause;
            }
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            this.bll = new GDBll();

            this.LeftMinutes = this.bll.GetLeftMinute();
        }

        /// <summary>
        /// 开始
        /// </summary>
        private void Start()
        {
            this.btnOpe.Text = "暂停";
            this.startTime = DateTime.Now;
            var preTime = this.startTime;

            if (this.taskStart == null || this.taskStart.IsCompleted)
            {
                this.taskStart = Task.Factory.StartNew(() =>
                {
                    while (true)
                    {
                        while((DateTime.Now - preTime).TotalSeconds < 60)
                        {
                            Thread.Sleep(1000);
                            if (this.EnmFrmMode != FrmModel.Start)
                            {
                                return;
                            }
                        }

                        preTime = DateTime.Now;
                        this.Invoke(new Action(() =>
                        {
                            this.SaveLeaned(this.startTime, DateTime.Now);
                            this.LeftMinutes = this.bll.GetLeftMinute();
                        }));
                    }
                });
            }
        }

        /// <summary>
        /// 暂停
        /// </summary>
        private void Pause()
        {
            this.btnOpe.Text = "开始";
            this.SaveLeaned(this.startTime, DateTime.Now);
        }

        /// <summary>
        /// 保存学习时间
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        private void SaveLeaned(DateTime start, DateTime end)
        {
            this.bll.SaveLeaned(start, end);
        }

        #endregion

        #region 定义

        /// <summary>
        /// 窗体状态
        /// </summary>
        enum FrmModel
        {
            Start,
            Pause
        }

        #endregion
    }
}
